<?php
namespace app\shawting\controller;

use think\Db;

class ClassifyInfo extends Base
{
    public function index()
    {
        $search = input('search')?input('search'):"";
        $where["A.context"] = ["like","%{$search}%"];
        $arr = db("city_classify_info")->alias("A")
            ->field("A.*,B.nick_name")
            ->join("city_user B","A.user_id=B.user_id")
            ->where(["A.delete_flag"=>1])
            ->where($where)
            ->paginate(10,false, ['query'=> request()->param()]/*不丢失已存在的url参数]*/);
        $sensitive = db("city_sensitive_words")->select();
        $this->assign("arr",$arr);
        $this->assign("search",$search);
        $this->assign("sensitive",$sensitive);
        return $this->fetch();
    }

    public function ajax_index_context(){
        $id = input('id');
        $context = input("context");
        if(!is_numeric($id) || $context == ''){
            return $this->error("内容不能为空");
        }
        $re = db("city_classify_info")->where(["id" => $id])->update(["context"=>$context]);
        if($re){
            return $this->success("编辑成功");
        }else{
            return $this->error("编辑失败");
        }
    }

    public function ajax_index_show(){
        $id = input('id');
        $recommend_flag = input('recommend_flag');
        if(!is_numeric($id) || !is_numeric($recommend_flag)){
            return $this->error("提交数据错误");
        }
        $count = db("city_classify_info")->where(["recommend_flag"=>$recommend_flag])->count();
        if($count >=3 ){
            return $this->error("已经超过最大推荐数量限制");
        }
        $re = db("city_classify_info")->where(["id" => $id])->update(["recommend_flag"=>$recommend_flag]);
        if($re){
            return $this->success("修改成功");
        }else{
            return $this->error("修改失败");
        }
    }

    public function ajax_index_delete(){
        $id = input("id");
        if(!is_numeric($id)){
            return $this->error("提交数据错误");
        }
        $re = db("city_classify_info")->where(['id'=>$id])->update(["delete_flag"=>0]);
        if($re){
            return $this->success("删除成功");
        }else{
            return $this->error("删除失败");
        }
    }

    public function sensitive(){
        $sensitive = input("sensitive");
        $sensitive = str_replace('*', '', $sensitive);
        $sensitive = explode("\r\n",$sensitive);
        if(!is_array($sensitive)){
            return $this->error("数据提交错误");
        }
        foreach ($sensitive as $key => $value){
            if(!$value){
                continue;
            }
            $data[$key]['words'] = trim($value," ");
        }
        Db::execute("truncate table t_city_sensitive_words");
        $re = db("city_sensitive_words")->insertAll($data);
        if($re){
            return $this->success("操作成功");
        }else{
            return $this->error("操作失败");
        }
    }
}